package com.snail.snailvr.views.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.snail.snailvr.R;
import com.snail.snailvr.widget.RadioButton;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 单选Dialog的Adapter
 * Created by snail on 16/5/4.
 */
public class SingleChoiceAdapter extends BaseAdapter {
    private String[]contents;
    private LayoutInflater mLayoutInflater;
    private int choicePosition;
    private Context mContext;
    private View.OnClickListener mOnClickListener;
    public SingleChoiceAdapter(Context context, View.OnClickListener onClickListener,String[]contents, int choicePosition){
        mContext=context;
        this.contents=contents;
        this.choicePosition=choicePosition;
        this.mOnClickListener=onClickListener;
        mLayoutInflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return contents.length;
    }

    @Override
    public String getItem(int position) {
        return contents[position];
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if(convertView==null){
            convertView=mLayoutInflater.inflate(R.layout.single_choice_item,parent,false);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder=(ViewHolder)convertView.getTag();
        }
        if(choicePosition==position){
            holder.mRadioButton.setEnabled(true);
            holder.mRadioButton.setChecked(true);
            holder.mTitle.setTextColor(mContext.getResources().getColor(R.color.positive_normal_color));
        }else{
            holder.mRadioButton.setChecked(false);
            holder.mRadioButton.setEnabled(false);
            holder.mTitle.setTextColor(mContext.getResources().getColor(R.color.toolbar_title_color));
        }

        holder.mTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClicked(v, position);
            }
        });
        holder.mSubTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClicked(v, position);
            }
        });
        String title=getItem(position);
        if(title!=null){
            holder.mTitle.setText(title);
        }

        return convertView;
    }

    private void onItemClicked(View v, int position) {
        choicePosition=position;
        notifyDataSetChanged();
        v.setTag(choicePosition);
        mOnClickListener.onClick(v);
    }

    class ViewHolder{
        @Bind(R.id.single_choice_title)
        TextView mTitle;
        @Bind(R.id.single_choice_subtitle)
        TextView mSubTitle;
        @Bind(R.id.radio_button)
        RadioButton mRadioButton;
        public ViewHolder(View itemView){
            ButterKnife.bind(this,itemView);
            mRadioButton.setClickable(false);
        }
    }


    public int getChoicePosition() {
        return choicePosition;
    }
}
